package com.gpscookie.mobile;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Image;

import org.json.me.JSONArray;
import org.json.me.JSONException;
import org.json.me.JSONObject;

import de.enough.polish.ui.ImageItem;
import de.enough.polish.ui.StringItem;
import de.enough.polish.util.Locale;

// http://ditu.google.cn/staticmap?size=240x320&maptype=mobile&markers=39.904667,116.408198,midgreen|39.904667,116.408198,midgreen|39.909542,116.411964,midgreen|39.910062,116.417174,midgreen|39.91009,116.416941,midgreen|39.910414,116.413086,midgreen|39.912398,116.413464,midgreen|39.914528,116.402747,midgreen|&key=ABQIAAAA3Jctzi6h9XjhBnp3WNhIJxQnE-HqWLobAj7gM0G2Up4_rw7kTBTHMO63dhhpN2Rp5h6NYa--6XHbWw&sensor=false
public class ViewScreen extends BasicScreen implements DownloadListener
{

	private DownloadManager dlManager = null;
	Command cmdFollow;
	private JSONObject mission = null;
	
	public ViewScreen(String title, GPSCookie globalClass) {
		super(title, globalClass);
		this.dlManager = new DownloadManager();
		this.cmdBack = new Command(Locale.get("BACK"), Command.SCREEN, 1);
		this.cmdFollow = new Command(Locale.get("FOLLOW"), Command.SCREEN, 10);
		this.addCommand(this.cmdBack);	
		this.addCommand(this.cmdFollow);
		this.parentScreen = GPSCookie.searchScreen;	
		this.appendAd();
	}

	public void commandAction(Command arg0, Displayable arg1) {
		if (arg0 == cmdBack) {
			this.goBack();
		} else if (arg0 == cmdFollow) {
			try {
				if ("public".equalsIgnoreCase(mission.getString("type").trim())) {
					this.globalClass.forumList(mission, "");
				} else {
					this.globalClass.follow(mission);
				}
				System.out.println("mission type: "+mission.getString("type"));
			} catch (JSONException e) {
				this.labInfo.setText(e.toString());
			}			
		}
	}
	
	private void cleanScreen() {
		this.deleteAll();
		//#style infoText
		this.labInfo = new StringItem(Locale.get("STATUS"), Locale.get("WAITMSG"));
		this.append(this.labInfo);		
	}
	
	public void download(String url) {
		this.cleanScreen();
		String appended = url;		
		this.dlManager.download(this, appended);
		System.out.println(appended);
	}

	public void downloadCompleted(byte[] data) {	
		String str;
		try {
			if (data==null || data.length<=0) {
				throw new Exception("Not found.");
			}
			str = GPSCookie.http2String(data);
			JSONObject json = new JSONObject(str);
			str = json.getString("msg");
			if ("OK".equals(str.toUpperCase())) {
				JSONObject item = json.getJSONObject("item");
				this.setItem(item);
			} else {
				throw new Exception(str);
			}
		} catch (Exception e) {
			this.labInfo.setText(e.toString());
			e.printStackTrace();			
		}		
	}
	
	protected void setItem(JSONObject mission) throws JSONException 
	{
		this.deleteAll();
		//#style stringItem
		StringItem name = new StringItem(Locale.get("TITLE"), mission.getString("name"));
		//#style stringItem
		StringItem description = new StringItem(Locale.get("DESCRIPTION"), mission.getString("description"));
		//#style stringItem
		StringItem location = new StringItem(Locale.get("LOCATION"), mission.getString("location"));
		//#style stringItem
		StringItem keeper = new StringItem(Locale.get("KEEPER"), mission.getString("keeper"));
		this.append(name);
		this.append(location);
		this.append(description);
		this.append(keeper);
		this.mission = mission;
	}
	
	public void downloadError(Exception e) {
		this.labInfo.setText(e.getMessage());	
	}

	public void downloadStatus(int percent) {
		this.labInfo.setText(String.valueOf(percent)+"%");		
	}
}
